package org.lds.ers.client.widgets;

import java.util.List;

import org.lds.ers.client.LoginService;
import org.lds.ers.client.LoginServiceAsync;
import org.lds.ers.client.StakeService;
import org.lds.ers.client.StakeServiceAsync;
import org.lds.ers.client.model.StakeInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

/*
 * 維護 Stake 的畫面
 */
public class MaintainStake extends VerticalPanel {
	private static MaintainStake singleton;
	public static MaintainStake getInstance()
	{
		if (singleton==null)
			singleton = new MaintainStake();
		return singleton ;
	}
	
	final private Label lblBanner;
	final private VerticalPanel pnl1;
	final private HorizontalPanel pnl2;
	
	private StakeServiceAsync stakeService = GWT.create(StakeService.class);
	
	private MaintainStake()
	{
		this.lblBanner = new Label("[[管理支聯會]]");
		this.add(this.lblBanner);

		//this.self = this;
		this.pnl1 = new VerticalPanel();
		this.add(pnl1);
		
		//Add "AddNew" button
		Button btn = new Button("新增支聯會");
		btn.addClickHandler( new ClickHandler() {
			public void onClick(ClickEvent event) {
				// TODO Show Dialog box in Add New mode !
				MaintainStakeSForm frm = new MaintainStakeSForm();
				frm.setAnimationEnabled(true);
				frm.center();
				frm.show();
			}
		});
		this.pnl1.add(btn);
		this.pnl2 = new HorizontalPanel();
		this.pnl1.add(pnl2);
		
		refreshTable();
	}
	
	public void refreshTable()
	{
		stakeService.getAll(new AsyncCallback<List<StakeInfo>>() {

			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert(caught.getMessage());
			}

			public void onSuccess(List<StakeInfo> result) {
				// TODO Auto-generated method stub
				redrawTable(result);
				MaintainStakeERS.getInstance().refreshStakes();
			}
	    });
	}
	
	private void redrawTable(List<StakeInfo> stakes)
	{
		this.pnl2.clear();
		//Create a Flex Table
		final FlexTable flexTable =new FlexTable();
	    
	    flexTable.addStyleName("cw-FlexTable");
	    flexTable.setWidth("32em");
	    flexTable.setBorderWidth(1);
	    flexTable.setCellSpacing(0);
	    flexTable.setCellPadding(3);
	    flexTable.setText(0, 0, "支聯會名稱" );
		flexTable.setText(0, 1, "支聯會代碼" );
		flexTable.setText(0, 2, "備註");
	    
	    int row = 1;
		for(StakeInfo s : stakes )
		{
			addRow(row, flexTable, s);
			row += 1;
		}
		this.pnl2.add(flexTable);
	}
	
	private void addRow(int row, FlexTable flexTable, StakeInfo s)
	{
		final StakeInfo ss = s;
		FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
		
		flexTable.setText(row, 0, s.getStakeName() );
		flexTable.setText(row, 1, s.getStakeNo() );
		flexTable.setText(row, 2, s.getRemark());
		
		HorizontalPanel pnl = new HorizontalPanel();
    	
    	Button btn = new Button("編輯");
    	btn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				//Show Dialog box in Add New mode !
				MaintainStakeSForm frm = new MaintainStakeSForm();
				frm.setStakeInfo(ss);
				frm.setAnimationEnabled(true);
				frm.center();
				frm.show();
			}
    	}
    	);
    	pnl.add(btn);
    	pnl.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
    	
    	final Button btn2 = new Button("刪除");
    	btn2.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				if (Window.confirm("您確定要刪除這個支聯會：『" +  ss.getStakeName() + "』嗎?"))
				{
					btn2.setEnabled(false);
					//Window.alert(ss.getId().toString());
					stakeService.delete(ss.getId(), new AsyncCallback<Boolean>() {
						public void onFailure(Throwable caught) {
							Window.alert("刪除失敗");
							btn2.setEnabled(true);
						}
			
						public void onSuccess(Boolean result) {
							MaintainStake.getInstance().refreshTable();
						}
					});
				}
			}
    	}
    	);
    	pnl.add(btn2);
    	pnl.setCellHorizontalAlignment(btn2, HasHorizontalAlignment.ALIGN_CENTER);
    	flexTable.setWidget(row,3 , pnl);
	}
}
